#include "sessiondetailwidget.h"

#include <QGridLayout>
#include <QLabel>
#include <QMessageBox>
#include <QPushButton>

#include "choosefrienddialog.h"
#include "debug.h"
#include "model/data.h"
#include "model/datacenter.h"
#include "widgetkit/basewidget.h"

using widgetkit::AvatarItem;

SessionDetailWidget::SessionDetailWidget(QWidget* parent, model::UserInfo* userInfo)
    : QDialog(parent), userInfo(userInfo)
{
    // 1.设置窗口基本属性
    this->setStyleSheet("QWidget { background-color: rgb(255, 255, 255); }");
    this->setFixedSize(300, 300);
    this->setWindowTitle("会话详情");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setAttribute(Qt::WA_DeleteOnClose);

    // 2.网格布局
    QGridLayout* layout = new QGridLayout();
    layout->setSpacing(10);
    layout->setContentsMargins(50, 0, 50, 0);
    this->setLayout(layout);

    // 2-1 拉人建群按钮
    AvatarItem* createGroupBtn = new AvatarItem(QIcon(":/resource/image/cross.png"), "添加");
    createGroupBtn->getAvatarBtn()->setStyleSheet("QPushButton { border: 1px solid rgb(218, 218, 218); }");
    layout->addWidget(createGroupBtn, 0, 0);

    // 2-2 当前用户头像
#if TEST_UI
    AvatarItem* curUser = new AvatarItem(QIcon(":/resource/image/girl.jpg"), "超级无敌大美");
    layout->addWidget(curUser, 0, 1);
#endif

    AvatarItem* curUser = new AvatarItem(userInfo->avatar, userInfo->nickname);
    layout->addWidget(curUser, 0, 1);

    // 2-3 删除好友按钮
    deleteFriendBtn = new QPushButton();
    deleteFriendBtn->setFixedHeight(50);
    deleteFriendBtn->setText("删除好友");
    deleteFriendBtn->setStyleSheet("QPushButton { border: 1px solid rgb(90, 90, 90); border-radius: 5px; } "
                                   "QPushButton:pressed { background-color: rgb(235, 235, 235); }");
    layout->addWidget(deleteFriendBtn, 1, 0, 1, 3);


    // 关联 "拉人建群" 信号槽
    connect(createGroupBtn->getAvatarBtn(), &QPushButton::clicked, this, [=](){
        ChooseFriendDialog* chooseFriendDialog = new ChooseFriendDialog(this, userInfo->userId);
        chooseFriendDialog->exec();
    });

    connect(deleteFriendBtn, &QPushButton::clicked, this, &SessionDetailWidget::onClickedBydeleleFrientBtn);
}

void SessionDetailWidget::onClickedBydeleleFrientBtn()
{
    QMessageBox::StandardButton ret = QMessageBox::warning(this, "确认删除", "是否确认删除当前好友", QMessageBox::Ok | QMessageBox::Cancel);
    if(ret != QMessageBox::Ok) {
        LOG(LL_INFO, "取消删除好友");
        return;
    }

    // 请求的响应处理函数是在主窗口中去操作的, 因此响应函数的信号槽关联置于主窗口内去完成
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    dataCenter->deleteFriendAsync(this->userInfo->userId);

    this->close();
}



